﻿/*-----------------------------------------------------------------------------!
! SupplierManager controller class                                             !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierManager.cs                                                !
! Function : controls all supplier functions                                   !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Sadharon;
using Model.Poribar;
using Model.Sohaok;
using Model.Poribar.SupplierPanjeri;
using Model.Panjeri;
using Model.Poribar.AddressPanjeri;
using Model.Upadan;

namespace InventorySys.controller
{
    public class SupplierController
    {
        #region private variable
        /// <summary>
        /// private method
        /// </summary>
        private ISupplierKaz supplierPanjeri;
        private Result result;
        #endregion

        #region constructor
        /// <summary>
        /// supplier controller constructor
        /// </summary>
        public SupplierController()
        {
            supplierPanjeri = Panjeri.getSupplierPanjeri();
            result = new Result();
        }
        #endregion


        #region add new supplier

        /// <summary>
        /// addNewSupplier add new supplier information with address          
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="address"></param>
        /// <returns></returns>        
        public Result addNewSupplier(Model.Upadan.Supplier supplier, Model.Upadan.Address address)
        {
            // save suppler information
            // validation
            if (supplier.AccountReference.Length == 0 || supplier.AccountReference == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Account reference is missing");
                return result;
            }

            if (supplier.CompanyName.Length == 0 || supplier.CompanyName == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Company name is missing");
                return result;
            }

            if (supplier.ContactPerson.Length == 0 || supplier.ContactPerson == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Contact person is missing");
                return result;
            }

            if (address.Country.Length == 0 || address.Country == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Country is missing");
                return result;
            }

            if (address.Description.Length == 0 || address.Description == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Address information is missing");
                return result;
            }

            if (address.Mobile.Length == 0 || address.Mobile == null)
            {
                result = Util.setSuccessResult(false, "Failed to add: Mobile information is missing");
                return result;
            }




            Result spResult = new Result();
            spResult = supplierPanjeri.add(supplier);
            if (spResult.IsSuccess == false)
            {
                result = spResult;
                return result;
            }
            result = spResult;
            supplier = (Model.Upadan.Supplier)result.ResultObj;

            // save address information
            address.PersonId = supplier.Id;
            Result adResult = new Result();
            IAddressKaz addressFunction = Panjeri.getAddressPanjeri();
            adResult = addressFunction.add(address);
            if (adResult.IsSuccess == false)
            {
                result = Util.setSuccessResult(true, "New Supplier added successfully, but failed to add " +
                "address information. You can add address information from Supplier list form");
                return result;
            }
            else
            {
                result = Util.setSuccessResult(true, "New Supplier added successfully", (object)supplier.Id);              
            }
            return result;
        }
        #endregion

        #region get all suppler list
        /// <summary>
        /// GetAllSupplier method
        /// </summary>
        /// <returns></returns>
        public Result GetAllSupplier()
        {
            return supplierPanjeri.getList();
        }
        #endregion

        #region get supplier details
        /// <summary>
        /// GetSupplierDetails according with supplier id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result GetSupplierDetails(string id)
        {
            return supplierPanjeri.getById(id);
        }

        #endregion

        #region update supplier details
        /// <summary>
        /// updateSupplierDetails
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public Result updateSupplierDetails(Supplier supplier, Address address)
        {
            // TODOS validation if required
            
            // update supplier information
            result = supplierPanjeri.update(supplier);
            if (result.IsSuccess)
            {
                AddressController addressController = new AddressController();
                if (addressController.updateAddressDetails(address).IsSuccess)
                {
                    result.Message = "Supplier details updated successfully";
                }
                else
                {
                    result.Message = "Failed to update address information. Please try later";
                }
            }
            return result;
        }

        #endregion

        #region delete supplier details
        /// <summary>
        /// deleteSupplierDetails
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result deleteSupplierDetails(string id)
        {
            return supplierPanjeri.delete(id);
        }

        #endregion

    }
}
